﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTimKiemPhieuThuTien : Form
    {
        private PhieuThuTienManagement phieuthutienMgt = new PhieuThuTienManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemPhieuThuTien()
        {
            InitializeComponent();
        }

        private void FrmTimKiemPhieuThuTien_Load(object sender, EventArgs e)
        {
            cboNoiThucHien.DataSource = GetAllChiNhanhCongTy();
            cboNoiThucHien.DisplayMember = "TenChiNhanh";
            cboNoiThucHien.ValueMember = "Id";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;


            List<PhieuThuTien> lstPhieuThuTien = (from ptt in GetAllPhieuThuTien()
                                                  where (ptt.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                        ptt.NguoiLap.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                        ptt.NguoiNopTien.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                        ptt.NgayThang.Date >= dtTuNgay.Date && ptt.NgayThang.Date <= dtDenNgay.Date &&
                                                        ptt.IdChiNhanhCongTy == Convert.ToInt32(cboNoiThucHien.SelectedValue.ToString())
                                                  select ptt).ToList();

            dgvDuyetPhieuThuTien.DataSource = lstPhieuThuTien;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(this.nhanvien);
            this.Hide();
            frmPhieuThuTien.ShowDialog();
            this.Show();
        }

        private void dgvDuyetPhieuThuTien_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PhieuThuTien phieuthutien = (PhieuThuTien)dgvDuyetPhieuThuTien.CurrentRow.DataBoundItem;
            if(phieuthutien != null)
            {
                FrmPhieuThuTien frmPhieuThuTien = new FrmPhieuThuTien(phieuthutien,this.nhanvien);
                frmPhieuThuTien.fromClosePhieuThuTien += new FormClosePhieuNhapKhoEventHandler(frmPhieuThuTien_fromClosePhieuThuTien);
                frmPhieuThuTien.Show();
            }
        }

        void frmPhieuThuTien_fromClosePhieuThuTien()
        {
            btnTimKiem_Click(null, null);
        }

        public List<PhieuThuTien> GetAllPhieuThuTien()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuthutienMgt.GetAllPhieuThuTien();
            List<PhieuThuTien> listPhieuThuTien = (from pt in(List<PhieuThuTien>)resultObj.Result
                                                       where pt.IsDelete==0
                                                       select pt).ToList();

            return listPhieuThuTien;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (from cn in(List<ChiNhanhCongTy>)result.Result
                                                          where cn.IsDelete==0
                                                          select cn).ToList();
            return lstChiNhanhCongTy;
        }
       
    }
}
